package com.pearadmin.pro.modules.project.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.tools.core.GeneralConvertUtils;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;

import com.pearadmin.pro.modules.project.domain.Project;
import com.pearadmin.pro.modules.project.domain.ProjectCharge;
import com.pearadmin.pro.modules.project.dto.ProjectDTO;
import com.pearadmin.pro.modules.project.param.ProjectRequest;
import com.pearadmin.pro.modules.project.repository.ProjectRepository;
import com.pearadmin.pro.modules.project.service.ProjectChargeService;
import com.pearadmin.pro.modules.project.service.ProjectService;
import com.pearadmin.pro.modules.school.domain.School;
import com.pearadmin.pro.modules.sys.domain.BusinessFile;
import com.pearadmin.pro.modules.sys.service.BusinessFileService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 项目信息Service业务层处理
 *
 * @author ChenJian
 * @date 2021-08-29
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectRepository, Project> implements ProjectService
{
    @Resource
    private ProjectRepository projectRepository;
    @Resource
    private BusinessFileService businessFileService;
    @Resource
    private ProjectChargeService projectChargeService;
    /**
     * 查询项目信息
     * @param request
     * @return 项目信息 分页集合
     * */
    @Override
    public PageResponse<Project> page(ProjectRequest request){
        LambdaQueryWrapper<Project> query = new LambdaQueryWrapper<Project>();
        query.like(ObjectUtil.isNotEmpty(request.getSchName()),Project::getSchName,request.getSchName());
        query.like(ObjectUtil.isNotEmpty(request.getProjectName()),Project::getProjectName,request.getProjectName());
        query.like(ObjectUtil.isNotEmpty(request.getResponsiblePersonName()),Project::getResponsiblePersonName,request.getResponsiblePersonName());
        query.ge(ObjectUtil.isNotEmpty(request.getStatCreateTime()),Project::getCreateTime,request.getStatCreateTime());
        query.le(ObjectUtil.isNotEmpty(request.getEndCreateTime()),Project::getCreateTime,request.getEndCreateTime());
        return Pageable.of(request,(()->  projectRepository.selectList(query)));
    }

    @Override
    public Boolean saveOrUpdate(ProjectDTO projectDTO) {
        Project project = GeneralConvertUtils.conv(projectDTO, Project.class);
        saveOrUpdate(project);
        if (!ObjectUtils.isEmpty(projectDTO.getProjectCharge())) {
            List<ProjectCharge> projectChargeList = GeneralConvertUtils.convert2List(projectDTO.getProjectCharge(), ProjectCharge.class);
            projectChargeList.stream().forEach(s -> {
                s.setProjectId(project.getId());
            });
            projectChargeService.saveOrUpdateBatch(projectChargeList);
        }
        if (!ObjectUtils.isEmpty(projectDTO.getFiles())) {
            List<BusinessFile> schoolSendList = GeneralConvertUtils.convert2List(projectDTO.getFiles(), BusinessFile.class);
            schoolSendList.stream().forEach(s -> {
                s.setBusinessId(project.getId());
            });
            businessFileService.saveOrUpdateBatch(schoolSendList);
        }
        return true;
    }

    /**
     * 查询项目信息
     *
     * @param id 项目信息ID
     * @return 项目信息
     */
    @Override
    public ProjectDTO selectProjectById(Long id)
    {

        ProjectDTO projectDTO = GeneralConvertUtils.conv(projectRepository.selectById(id),ProjectDTO.class);

        projectDTO.setProjectCharge(projectChargeService.selectProjectChargeId(projectDTO.getId()));

        LambdaQueryWrapper<BusinessFile> queryBusinessFile = new LambdaQueryWrapper<>();
        queryBusinessFile.eq(BusinessFile::getBusinessId, id);
        projectDTO.setFiles(businessFileService.list(queryBusinessFile));


        return projectDTO;
    }

}
